package com.intel.two.service.building.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intel.two.common.LayuiPageVo;
import com.intel.two.mapper.buildingMapper.EquipmentMapper;
import com.intel.two.pojo.building.Equipment;
import com.intel.two.service.building.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("equipmentService")
public class EquipmentServiceImpl implements EquipmentService {
    @Autowired
    private EquipmentMapper equipmentMapper;

    @Override
    public LayuiPageVo<Equipment> equipmentPageData(Map<String, Object> params) {
        //mybatis 分页 插件的使用
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");

        PageHelper.startPage(page,limit);
        List<Equipment> equipmentList = equipmentMapper.equipmentPageData(params);
        System.out.println(equipmentList);

        PageInfo<Equipment> equipmentPageInfo = new PageInfo<>(equipmentList);

        LayuiPageVo<Equipment> equipmentLayuiPageVo = new LayuiPageVo<>();
        equipmentLayuiPageVo.setCode(0);
        equipmentLayuiPageVo.setMsg("消防器材列表");
        equipmentLayuiPageVo.setCount(equipmentPageInfo.getTotal());
        equipmentLayuiPageVo.setData(equipmentPageInfo.getList());
        return equipmentLayuiPageVo;
    }

    @Override
    public int addEquipment(Equipment equipment) {
        return equipmentMapper.addEquipment(equipment);
    }

    @Override
    public int deleteEquipment(Integer id) {
        return equipmentMapper.deleteEquipment(id);
    }

    @Override
    public int updateEquipment(Equipment equipment) {
        return equipmentMapper.updateEquipment(equipment);
    }

    @Override
    public int deleteEquipments(Map<String, Object> map) {
        String str = (String) map.get("id");
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
            System.out.println(strs[i]);
        }
        return equipmentMapper.deleteEquipments(list);
    }
}
